package com.itheima.exerciseTest.test30_39;

        import java.util.*;

/*
    定义一个泛型为String类型的List集合，统计该集合中每个字符（注意，不是字符串）出现的次数。
例如：集合中有”abc”、”bcd”两个元素，程序最终输出结果为：“a = 1,b = 2,c = 2,d = 1”。
 */
public class Test36 {
    public static void main(String[] args) {
        ArrayList<String> al = new ArrayList<>();
        // 添加元素
        addPlus(al, 8, 50);
        // 打印添加后的集合
        printList(al);
        // 定义TreeMap集合
        TreeMap<String,Integer> tm = new TreeMap<>((String s1 ,String s2) -> s1.compareTo(s2));
        // 创建StringBuilder拼接ArrayList元素
        StringBuilder sb = new StringBuilder();
        for (String s : al) {
            sb.append(s);
        }
        String str = sb.toString();
        char[] arr = str.toCharArray();
        for (int i = 0; i < arr.length; i++) {
            String tamp1 = arr[i] + "";
            int count = 0;
            for (int j = 0; j < arr.length; j++) {
                String tamp2 = arr[j] + "";
                if(tamp1.equals(tamp2))
                    count++;
            }
            tm.put(tamp1,count);
        }

        // 打印
        Set<String> keys = tm.keySet();
        for (String key : keys) {
            Integer value = tm.get(key);
            System.out.println(key + " = " + value);
        }


    }

    // 添加集合元素
    public static void addPlus(List<? super String> list, int lengh, int number) {
        Random r = new Random();
        for (int i = 0; i < number; i++) {
            String tamp = "";
            int count = r.nextInt(lengh) + 1;
            while (count > 0) {
                int ran = r.nextInt(75) + 48;
                if (ran >= 'a' && ran <= 'z') {
                    tamp += (char) ran;
                    count--;
                }else if (ran >= 'A' && ran <= 'Z') {
                    tamp += (char) ran;
                    count--;
                }

            }
            list.add(tamp);
        }
    }

    // 打印集合
    public static void printList(List list) {
        System.out.print("[");
        for (int i = 0; i < list.size() - 1; i++) {
            System.out.print(list.get(i) + ", ");
        }
        System.out.println(list.get(list.size() - 1) + "]");
    }
}
